<template>
  <viwe>
    <!-- 搜索框 -->
    <view class="search-box">
      <my-search-bar />
    </view>
    <!-- 轮播图 -->
    <swiper indicator-dots autoplay circular>
      <swiper-item v-for="item in swiper" :key="item.good_id">
        <image :src="item.image_src" mode="scaleToFill" />
      </swiper-item>
    </swiper>
    <!-- 导航菜单 -->
    <view class="nav-list">
      <image
        class="nav-img"
        v-for="item in cattimes"
        :key="item.name"
        :src="item.image_src"
        mode="scaleToFill"
        @click="gocategory(item.name)"
      />
    </view>
    <!-- 楼层 -->
    <view v-for="item in floorDate" :key="item.floor_title.name">
      <!-- 标题 -->
      <image
        class="floor-title"
        :src="item.floor_title.image_src"
        mode="scaleToFill"
      />
      <!-- 内容部分 -->
      <view class="floor-img-box">
        <!-- 左侧 -->
        <navigator :url="item.product_list[0].navigator_url">
          <view>
            <image
              :src="item.product_list[0].image_src"
              mode="widthFix"
              :style="{ width: item.product_list[0].image_width + 'rpx' }"
            />
          </view>
        </navigator>
        <!-- 右侧 -->

        <view class="right-img-box">
          <navigator
            :url="subitem.navigator_url"
            v-for="(subitem, index) in item.product_list"
            :key="subitem.name"
          >
            <image
              v-show="index > 0"
              :src="subitem.image_src"
              mode="widthFix"
              :style="{ width: subitem.image_width + 'rpx' }"
            />
          </navigator>
        </view>
      </view>
    </view>
  </viwe>
</template>

<script>
export default {
  data() {
    return {
      swiper: [],
      cattimes: [],
      floorDate: [],
    };
  },
  onLoad() {
    this.getswiperData();
    this.getcattimesData(), this.getfloorDate();
  },
  methods: {
    async getswiperData() {
      const res = await uni.$request({
        url: "home/swiperdata",
      });

      //  console.log(res);
      if (res.meta.status === 200) {
        this.swiper = res.message;
      }
    },
    async getcattimesData() {
      const res = await uni.$request({
        url: "home/catitems",
      });
      // console.log(res);
      if (res.meta.status === 200) {
        this.cattimes = res.message;
      }
    },
    gocategory(name) {
      if (name === "分类") {
        uni.switchTab({ url: "/pages/category/index" });
      }
    },
    async getfloorDate() {
      const res = await uni.$request({
        url: "home/floordata",
      });
      console.log(res);
      if (res.meta.status === 200) {
        res.message.forEach((item) => {
          item.product_list.forEach((subitem) => {
            subitem.navigator_url =
              "/subpack/goods-list/index?" +
              subitem.navigator_url.split("?")[1];
          });
        });
        this.floorDate = res.message;
      }
    },
  },
};
</script>

<style lang="scss" scoped>
swiper {
  height: 400rpx;

  .swiper-item,
  image {
    width: 100%;
    height: 100%;
  }
}
.nav-list {
  display: flex;
  justify-content: space-around;
  margin: 15px 0;

  .nav-img {
    width: 128rpx;
    height: 140rpx;
  }
}
.floor-title {
  height: 60rpx;
  width: 100%;
  display: flex;
}

.right-img-box {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-around;
}

.floor-img-box {
  display: flex;
  padding-left: 10rpx;
}
.search-box {
  position: sticky;
  top: 0;
  z-index: 999;
}
</style>